package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.impl.UserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @GetMapping
    public UserInfo get(String id){

        return userInfoService.get(id);
    }


    @GetMapping("/checkPhone")
    public boolean checkPhone(String phone){

        boolean blen = userInfoService.checkPhone(phone);

        return !blen;
    }

    @GetMapping("/sendVerifyCode")
    public JsonResult sendVerifyCode(String phone){

        userInfoRedisService.sendVerifyCode(phone);

        return JsonResult.success();
    }


    @GetMapping("/checkUsername")
    public JsonResult checkUsername(String name){

        if (!StringUtils.hasLength(name)){

            return JsonResult.error(JsonResult.CODE_ERROR, "用户名不能为空", null);
        }

        UserInfo userInfo = userInfoService.checkUsername(name);

        if (userInfo == null){
            return JsonResult.success();
        }

        return JsonResult.error(JsonResult.CODE_ERROR, "用户名已存在,请重新输入", null);

    }

    @GetMapping("/checkPassword")
    public JsonResult checkPassword(String password, String rpassword){

        if (!StringUtils.hasLength(rpassword)){
            return JsonResult.error(2, "密码不能为空", "");
        }

        if (password.equals(rpassword)){
            return JsonResult.success();
        }

        return JsonResult.error(1, "密码不一致", "密码不一致");
    }


    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode){

        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);

        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username, String password){

        UserInfo user = userInfoService.login(username, password);

        String token = userInfoRedisService.token(user);

        HashMap<String, Object> map = new HashMap<>();

        map.put("token", token);
        map.put("user", user);

        return JsonResult.success(map);
    }


    @GetMapping("/currentUser")
    @RequireLogin
    public Object currentUser(HttpServletRequest request){

        String token = request.getHeader("token");

        UserInfo info = userInfoRedisService.getUserInfoByToken(token);

        return JsonResult.success(info);

    }

    @GetMapping("/strategies/favor")
    public Object strategiesFavor(String userId){

        List<String> list = userInfoRedisService.queryStrategyFavorId(userId);

        return JsonResult.success(list);
    }



}
